/**
 * SpringMVC restful 风格
 * 下载中心控制器，控制关于下载中心的一些业务逻辑操作
 * @author yanwl
 * @date 2014-08-20
 */
package com.chnbst.bstyun.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.GoodsDownloadCenter;
import com.chnbst.bstyun.model.pojo.Menu;
import com.chnbst.bstyun.model.vo.GoodsDownloadVo;
import com.chnbst.bstyun.service.GoodsDownloadCenterService;
import com.chnbst.bstyun.service.GoodsDownloadService;
import com.chnbst.bstyun.service.MenuService;

@Controller
@RequestMapping("/download")
public class DownloadController {
	/**
	 * 页数
	 */
	private int page = 1;
	/**
	 * 每页显示的条数
	 */
	private int pageSize = 4;
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private MenuService menuService;
	@Autowired
	private GoodsDownloadService goodsDownloadService;
	@Autowired
	private GoodsDownloadCenterService goodsDownloadCenterService;
	
	/**
	 * http://xxx.com/download
	 * get请求 返回下载中心首页
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView download(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
	
		/**
		 * 下载中心产品总记录数
		 */
		int totalRows = goodsDownloadService.selectAllGridsCount();
		
		String pg = request.getParameter("page");
		if(SignUtil.isNotEmpty(pg)) {
			page = Integer.valueOf(pg);
		}else {
			page = 1;
		}
		
		/**
		 * 下载中心产品总页数
		 */
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < page) {
			page = pageCount;
		}
		
		/**
		 * 当前页开始下标
		 */
		int start = Pager.getStart(page, pageSize);
		
		/**
		 * 分页显示下载中心的产品
		 * @param start 该页面的起始位置
		 * @param pageSize 每页显示的条数
		 */
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("start", start);
		map1.put("pageSize", pageSize);
		map1.put("sortField", "create_time");
		map1.put("sortOrder", "desc");
		List<GoodsDownloadVo> goodsDownloadList = goodsDownloadService.selectAllGrids(map1);
		List<GoodsDownloadCenter> goodsDownloadCenterList = goodsDownloadCenterService.selectGrid();

		for(int i=0;i<goodsDownloadList.size();i++){
			GoodsDownloadVo goodsDownloadVo = goodsDownloadList.get(i);
			goodsDownloadVo.setIsAndroid(1);
			goodsDownloadVo.setIsIos(1);
			goodsDownloadVo.setIsPc(1);
			goodsDownloadVo.setIsInk(1);
			goodsDownloadVo.setAndroidUrl("");
			goodsDownloadVo.setIosUrl("");
			goodsDownloadVo.setPcUrl("");
			goodsDownloadVo.setInkUrl("");
			goodsDownloadVo.setAndroidImageUrl("");
			goodsDownloadVo.setIosImageUrl("");
			String goodsDownloadId = goodsDownloadVo.getGoodsDownloadId();
			for(int j=0;j<goodsDownloadCenterList.size();j++){
				GoodsDownloadCenter  goodsDownloadCenter = goodsDownloadCenterList.get(j);
				String tmpID = goodsDownloadCenter.getGoodsDownloadId();
				int wayId = goodsDownloadCenter.getDownloadWayId();
				String downUrl = goodsDownloadCenter.getDownloadUrl();
				String imageUrl = goodsDownloadCenter.getDownloadImageUrl();
				if(goodsDownloadId.equals(tmpID)){
					if(wayId == 1){
						goodsDownloadVo.setIsAndroid(0);
						goodsDownloadVo.setAndroidUrl(downUrl);
						goodsDownloadVo.setAndroidImageUrl(imageUrl);
					}else if(wayId == 2){
						goodsDownloadVo.setIsIos(0);
						goodsDownloadVo.setIosUrl(downUrl);
						goodsDownloadVo.setIosImageUrl(imageUrl);
					}else if(wayId == 3){
						goodsDownloadVo.setIsPc(0);
						goodsDownloadVo.setPcUrl(downUrl);
					}else if(wayId == 4){
						goodsDownloadVo.setIsInk(0);
						goodsDownloadVo.setInkUrl(downUrl);
					}
				}
				
			}
		}
		
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.addObject("goodsDownloadList", goodsDownloadList);
		mav.addObject("page", page);
		mav.addObject("pageCount", pageCount);
		mav.addObject("totalRows", totalRows);
		mav.addObject("pageSize", pageSize);
		mav.setViewName("download/download");
		return mav;
	}
	
	/**
	 * ajax请求查询下载产品
	 * @param request
	 * @return 下载产品页面上的数据集合Map
	 */
	@RequestMapping(value="/ajaxPage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ajaxPage(HttpServletRequest request){
		/**
		 * 下载中心产品总记录数
		 */
		int totalRows = goodsDownloadService.selectAllGridsCount();
		
		String pg = request.getParameter("page");
		if(SignUtil.isNotEmpty(pg)) {
			page = Integer.valueOf(pg);
		}else {
			page = 1;
		}
		
		/**
		 * 下载中心产品总页数
		 */
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < page) {
			page = pageCount;
		}
		
		/**
		 * 当前页开始下标
		 */
		int start = Pager.getStart(page, pageSize);
		
		/**
		 * 分页显示下载中心的产品
		 * @param start 该页面的起始位置
		 * @param pageSize 每页显示的条数
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "create_time");
		map.put("sortOrder", "desc");
		List<GoodsDownloadVo> goodsDownloadList = goodsDownloadService.selectAllGrids(map);
		List<GoodsDownloadCenter> goodsDownloadCenterList = goodsDownloadCenterService.selectGrid();

		for(int i=0;i<goodsDownloadList.size();i++){
			GoodsDownloadVo goodsDownloadVo = goodsDownloadList.get(i);
			goodsDownloadVo.setIsAndroid(1);
			goodsDownloadVo.setIsIos(1);
			goodsDownloadVo.setIsPc(1);
			goodsDownloadVo.setIsInk(1);
			goodsDownloadVo.setAndroidUrl("");
			goodsDownloadVo.setIosUrl("");
			goodsDownloadVo.setPcUrl("");
			goodsDownloadVo.setInkUrl("");
			goodsDownloadVo.setAndroidImageUrl("");
			goodsDownloadVo.setIosImageUrl("");
			String goodsDownloadId = goodsDownloadVo.getGoodsDownloadId();
			for(int j=0;j<goodsDownloadCenterList.size();j++){
				GoodsDownloadCenter  goodsDownloadCenter = goodsDownloadCenterList.get(j);
				String tmpID = goodsDownloadCenter.getGoodsDownloadId();
				int wayId = goodsDownloadCenter.getDownloadWayId();
				String downUrl = goodsDownloadCenter.getDownloadUrl();
				String imageUrl = goodsDownloadCenter.getDownloadImageUrl();
				if(goodsDownloadId.equals(tmpID)){
					if(wayId == 1){
						goodsDownloadVo.setIsAndroid(0);
						goodsDownloadVo.setAndroidUrl(downUrl);
						goodsDownloadVo.setAndroidImageUrl(imageUrl);
					}else if(wayId == 2){
						goodsDownloadVo.setIsIos(0);
						goodsDownloadVo.setIosUrl(downUrl);
						goodsDownloadVo.setIosImageUrl(imageUrl);
					}else if(wayId == 3){
						goodsDownloadVo.setIsPc(0);
						goodsDownloadVo.setPcUrl(downUrl);
					}else if(wayId == 4){
						goodsDownloadVo.setIsInk(0);
						goodsDownloadVo.setInkUrl(downUrl);
					}
				}
				
			}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("page", page);
		resultMap.put("goodsDownloadList", goodsDownloadList);
		return resultMap;
	}
}

	
